#!/bin/bash

sudo apt update
sudo apt upgrade -y

# Wine的64位依赖。box64会将amd64调用转换为arm64，所以只需安装arm64库即可
# 依赖来自 dpkg-deb -I wine-stable-amd64_8.0.2~bookworm-1_amd64.deb
sudo apt install -y libasound2:arm64 libc6:arm64 libglib2.0-0:arm64 libgphoto2-6:arm64 libgphoto2-port12:arm64 \
  libgstreamer-plugins-base1.0-0:arm64 libgstreamer1.0-0:arm64 libpcap0.8:arm64 libpulse0:arm64 libsane1:arm64 \
  libudev1:arm64 libunwind8:arm64 libusb-1.0-0:arm64 libx11-6:arm64 libxext6:arm64 ocl-icd-libopencl1:arm64 \
  libasound2-plugins:arm64 libncurses6:arm64 libcapi20-3:arm64 libcups2:arm64 libdbus-1-3:arm64 libfontconfig1:arm64 \
  libfreetype6:arm64 libglu1-mesa:arm64 libgnutls30:arm64 libgsm1:arm64 libgssapi-krb5-2:arm64 libjpeg62-turbo:arm64 \
  libkrb5-3:arm64 libodbc1:arm64 libosmesa6:arm64 libpng16-16:arm64 libsdl2-2.0-0:arm64 libv4l-0:arm64 \
  libxcomposite1:arm64 libxcursor1:arm64 libxfixes3:arm64 libxi6:arm64 libxinerama1:arm64 libxrandr2:arm64 \
  libxrender1:arm64 libxslt1.1:arm64 libxxf86vm1:arm64

# Wine的32位依赖。同理安装armhf而不是i386
# 依赖来自 dpkg-deb -I wine-stable-i386_8.0.2~bookworm-1_i386.deb
sudo apt install -y libasound2:armhf libc6:armhf libglib2.0-0:armhf libgphoto2-6:armhf libgphoto2-port12:armhf \
  libgstreamer-plugins-base1.0-0:armhf libgstreamer1.0-0:armhf libpcap0.8:armhf libpulse0:armhf libsane1:armhf \
  libudev1:armhf libusb-1.0-0:armhf libx11-6:armhf libxext6:armhf ocl-icd-libopencl1:armhf libasound2-plugins:armhf \
  libncurses6:armhf libcapi20-3:armhf libcups2:armhf libdbus-1-3:armhf libfontconfig1:armhf libfreetype6:armhf \
  libglu1-mesa:armhf libgnutls30:armhf libgsm1:armhf libgssapi-krb5-2:armhf libjpeg62-turbo:armhf libkrb5-3:armhf \
  libodbc1:armhf libosmesa6:armhf libpng16-16:armhf libsdl2-2.0-0:armhf libv4l-0:armhf libxcomposite1:armhf \
  libxcursor1:armhf libxfixes3:armhf libxi6:armhf libxinerama1:armhf libxrandr2:armhf libxrender1:armhf libxslt1.1:armhf \
  libxxf86vm1:armhf

rm -rf $HOME/.local/share/tiny/tmp
mkdir $HOME/.local/share/tiny/tmp
cd $HOME/.local/share/tiny/tmp

LNKA="https://mirrors.tuna.tsinghua.edu.cn/wine-builds/debian/dists/bookworm/main/binary-amd64/"
DEB_A1="wine-stable-amd64_8.0.2~bookworm-1_amd64.deb"
DEB_A2="wine-stable_8.0.2~bookworm-1_amd64.deb"

LNKB="https://mirrors.tuna.tsinghua.edu.cn/wine-builds/debian/dists/bookworm/main/binary-i386/"
DEB_B1="wine-stable-i386_8.0.2~bookworm-1_i386.deb"

# Install amd64-wine (64-bit) alongside i386-wine (32-bit)
echo -e "下载wine..."
wget ${LNKA}${DEB_A1}
wget ${LNKA}${DEB_A2}
wget ${LNKB}${DEB_B1}

wget https://mirror.ghproxy.com/https://raw.githubusercontent.com/Winetricks/winetricks/master/src/winetricks
if [ $? -ne 0 ]; then
  wget https://raw.githubusercontent.com/Winetricks/winetricks/master/src/winetricks
  if [ $? -ne 0 ]; then
    rm -rf $HOME/.local/share/tiny/tmp
    echo '下载失败...退出安装...'
    exit
  fi
fi

echo -e "解压wine..."
dpkg-deb -x ${DEB_A1} wine-installer
dpkg-deb -x ${DEB_A2} wine-installer
dpkg-deb -x ${DEB_B1} wine-installer
echo -e "安装wine..."
mv wine-installer/opt/wine* ../cross/wine

mv winetricks ../cross
chmod +x $HOME/.local/share/tiny/cross/winetricks
cd ..
rm -rf tmp

echo '[Desktop Entry]
Name=Wine 启动器
Exec=wine start /unix %f
Icon=wine
Type=Application' > $HOME/.local/share/tiny/cross/wine.desktop
chmod +x $HOME/.local/share/tiny/cross/wine.desktop

echo '[Desktop Entry]
Name=winetricks 实用工具
Exec=winetricks
Icon=wine
Type=Application' > $HOME/.local/share/tiny/cross/winetricks.desktop
chmod +x $HOME/.local/share/tiny/cross/winetricks.desktop

echo '安装完成，在启用wine后可以点击exe文件选择用wine启动。'
echo '程序的运行可能依赖一些配置或组件。比如，一些使用directx的游戏可能需要安装dxvk。'
echo '如果缺失可以尝试使用winetricks安装。请务必保持耐心。'
